package com.jtjrenren.android.taxi.driver.core.splash;

import com.jtjrenren.android.taxi.driver.bean.event.DownLoadEvent;
import com.jtjrenren.android.taxi.driver.bean.event.UpEvent;

import com.jtjrenren.android.taxi.driver.bean.api.ApiUpVersion;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.utils.LogUtil;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * Created by Administrator on 2016/8/17.
 */

public class SplashInteractorImpl implements SplashInteractor {

    @Override
    public void checkAppVersion() {
        ApiWorks.checkAppVersion(new ApiWorks.ResponseListener<ApiUpVersion>() {
            @Override
            public void onResponse(ApiUpVersion upVersion) {
                EventBus.getDefault().post(new UpEvent(upVersion));
            }
        });
    }

    @Override
    public void downApp(String url, String path) {
        File file = new File(path);
        if (!file.exists())
            file.mkdirs();
        path = path + File.separator + "temp.apk";
        file = new File(path);
        if (file.exists())
            file.delete();
        LogUtil.log(SplashInteractorImpl.class, "down app:" + path + "---" + url);
        FileDownloader.getImpl().create(url).setPath(path)
                .setListener(new FileDownloadListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        int process = (int) ((soFarBytes / (float) totalBytes) * 100);
                        LogUtil.log(SplashInteractorImpl.class, "down progress:" + process);
                        DownLoadEvent downLoadEvent = new DownLoadEvent(DownLoadEvent.TYPE_DOWNING);
                        downLoadEvent.setProcess(process);
                        EventBus.getDefault().post(downLoadEvent);
                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        LogUtil.log(SplashInteractorImpl.class, "down completed:" + task.getPath());
                        DownLoadEvent downLoadEvent = new DownLoadEvent(DownLoadEvent.TYPE_DOWNFINISH);
                        downLoadEvent.setApk_path(task.getPath());
                        EventBus.getDefault().post(downLoadEvent);
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        LogUtil.log(SplashInteractorImpl.class, "down error:" + e.toString());
                        DownLoadEvent downLoadEvent = new DownLoadEvent(DownLoadEvent.TYPE_ERROR);
                        downLoadEvent.setErrorMsg(e.toString());
                        EventBus.getDefault().post(downLoadEvent);
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {
                        LogUtil.log(SplashInteractorImpl.class, "down warn:");
                    }
                }).start();
    }
}
